package org.jiuling.springboot.oauth2.resource.configuration;

import org.jiuling.springboot.oauth2.resource.entrypoint.DefaultAuthenticationEntryPoint;
import org.jiuling.springboot.oauth2.resource.handler.DefaultAccessDeniedHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

/**
 * 资源服务配置
 * @author 紫灵
 * @date 2022年06月06日 10时43分46秒
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceServerConfigConfiguration {

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(authorizeRequests ->
                        authorizeRequests.antMatchers("/**").permitAll().anyRequest().authenticated()
                )
                .oauth2ResourceServer()
                .accessDeniedHandler(new DefaultAccessDeniedHandler())
                .authenticationEntryPoint(new DefaultAuthenticationEntryPoint())
                .jwt();
        return http.build();
    }

}
